package com.tarzan.video.controller;

import com.tarzan.common.core.base.BaseResponse;
import com.tarzan.common.core.exception.BusinessException;
import com.tarzan.video.service.VideoService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author liujinshan
 * @title: sss
 * @projectName tarzan-cloud
 * @description: TODO
 * @date 2021-5-23 21:57
 */
@Api(tags = "MinIO对象存储管理")
@RestController
@RequestMapping("vid")
@Slf4j
public class VideoController {

    @Autowired
    private VideoService videoService;

    /** @title uploadVideo
     * @description TODO 视频上传功能
     * @param file
     * @return com.tarzan.common.core.base.BaseResponse
     * @throws
     * @author liujinshan
     * @data 2021/5/24 14:44
     */
    @PostMapping("video/upload")
    public BaseResponse<String> uploadVideo(@RequestParam("file") MultipartFile file){
        BaseResponse<String> response = new BaseResponse<>();
        try{
            response = BaseResponse.ok(videoService.uploadVideo(file));
        } catch (BusinessException e){
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            return BaseResponse.error("500",e.getMessage());
        }
        return response;
    }

    /** @title uploadVideo
     * @description TODO 视频上传功能
     * @param file
     * @return com.tarzan.common.core.base.BaseResponse
     * @throws
     * @author liujinshan
     * @data 2021/5/24 14:44
     */
    @PostMapping("img/upload")
    public BaseResponse<String> uploadImg(@RequestParam("file") MultipartFile file){
        BaseResponse<String> response = new BaseResponse<>();
        try{
            response = BaseResponse.ok(videoService.uploadImg(file));
        } catch (BusinessException e){
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            return BaseResponse.error("500",e.getMessage());
        }
        return response;
    }


    /**
     * @title deleteVideo
     * @description TODO 删除视频
     * @param objectName
     * @return com.tarzan.common.core.base.BaseResponse
     * @author liujinshan
     * @data 2021/6/18 16:13
     */
    @DeleteMapping("video/delete")
    public BaseResponse deleteVideo(String objectName){
        try{
            videoService.deleteVideo(objectName);
        } catch (BusinessException e){
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }


    /**
     * @title getUrlById
     * @description TODO 根据对应名称获取对应的url 链接地址
     * @param objectName
     * @return com.tarzan.common.core.base.BaseResponse<java.lang.String>
     * @author liujinshan
     * @data 2021/8/25 15:33
     */
    @GetMapping("video/getUrlById/{objectName}")
    public BaseResponse<String> getUrlById(@PathVariable String objectName){
        BaseResponse<String> response = new BaseResponse<>();
        try{
            response = BaseResponse.ok(videoService.getUrlById(objectName));
        } catch (BusinessException e){
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            return BaseResponse.error("500",e.getMessage());
        }
        return response;
    }
}
